package com.coollang.smartbra.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.coollang.smartbra.R;
import com.coollang.smartbra.adapter.NewTimeActionDetailsAdapter;
import com.coollang.smartbra.beans.NewActionData;
import com.coollang.smartbra.constant.Constants;
import com.coollang.smartbra.utils.TimeUtils;
import com.coollang.smartbra.utils.TranslucentSystemBarUtils;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author: Timmy
 * @data: 2017/12/19
 * @Fuction:
 */

public class NewTimeActionDetailsActivity extends AppCompatActivity {

    @BindView(R.id.status_bar)
    View mStatusBar;
    @BindView(R.id.content)
    LinearLayout content;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerView;
    @BindView(R.id.iv_back)
    ImageView mIvBack;
    @BindView(R.id.correct_time)
    TextView mTvCorrectTime;
    @BindView(R.id.error_time)
    TextView mTvErrorTime;

    private ArrayList<NewActionData> mActionDatas;
    private NewTimeActionDetailsAdapter mDetailsAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_time_action_details);
        ButterKnife.bind(this);
        TranslucentSystemBarUtils.showChenJinZhuangTaiLan(mStatusBar, this, content);

        processIntent();
        initView();
        initData();
    }

    private void processIntent() {
        Intent intent = getIntent();
        if (intent != null) {
            mActionDatas = getIntent().getParcelableArrayListExtra(Constants.LIST);
        }
    }

    private void initView() {
        mIvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tvTitle.setText("动作详情");
        tvTitle.setTextColor(ContextCompat.getColor(this, R.color.black_666));

        mDetailsAdapter = new NewTimeActionDetailsAdapter(null);
        // 取消RecycleView的动画
        mDetailsAdapter.isFirstOnly(false);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(mDetailsAdapter);
        mDetailsAdapter.addData(mActionDatas);
    }

    private void initData() {
        // 计算正确动作的个数
        long correctTime = 0;
        long errorTime = 0;
        for (NewActionData actionData : mActionDatas) {
            int time = actionData.getTime();
            int actionFlag = actionData.getActionFlag();
            if (actionFlag == 1) {
                correctTime += time;
            } else if (actionFlag == 2) {
                errorTime += time;
            }
        }

        mTvCorrectTime.setText(TimeUtils.getFormatmillisecond(correctTime));
        mTvCorrectTime.setPadding(0, 0, 0, 0);
        String text = "不规范时长 " + TimeUtils.getFormatmillisecond(errorTime);
        setError(text);
        mTvErrorTime.setPadding(0, 0, 0, 0);
    }

    private void setError(String text) {
        SpannableString msp = new SpannableString(text);
        // 设置字体大小
        msp.setSpan(new RelativeSizeSpan(1.25f), 5, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        mTvErrorTime.setText(msp);
    }
}
